﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace FI.Core.Utilities
{
    /// <summary>
    /// 
    /// </summary>
    public static class TypeExtensions
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public static string GetPropertyName(this Type type, string propertyName)
        {
            var props = type.GetProperties();
            var prop =
                props.FirstOrDefault(
                    it => string.Equals(it.Name, propertyName, StringComparison.InvariantCultureIgnoreCase));
            if (prop != null)
                return prop.Name;
            var message = String.Format("'{0}' does not contain a definition for '{1}'.", type.Name, propertyName);
            throw new ArgumentException(message, propertyName);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static PropertyInfo GetProperty2(this Type type, string propertyName)
        {
            var prop = GetPropertyName(type, propertyName);
            return type.GetProperty(prop);
        }
    }
}
